/****************************************************************************
** Licensed Materials - Property of IBM
** 
** Governed under the terms of the International
** License Agreement for Non-Warranted Sample Code.
**
** (C) COPYRIGHT International Business Machines Corp. 2006
** All Rights Reserved.
**
** US Government Users Restricted Rights - Use, duplication or
** disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
*****************************************************************************
**
** SOURCE FILE NAME: insnapnew.C
**
** SAMPLE: Capture a snapshot at the instance level
**          
**         This sample creates an instance attachment, enables throttling
**         for utilities and calls functions in utilsnap.c to capture an
**         instance-level snapshot and print the monitor data.
**
** DB2 APIs USED:
**         db2GetSnapshot -- Get Snapshot
**
** STRUCTURES USED:
**         db2AddSnapshotRqstData
**
** OUTPUT FILE: insnap.out (available in the online documentation)
*****************************************************************************
**
** For more information on the sample programs, see the README file.
**
** For information on developing C++ applications, see the Application
** Development Guide.
**
** For information on DB2 APIs, see the Administrative API Reference.
**
** For the latest information on programming, compiling, and running DB2
** applications, visit the DB2 application development website at
**     http://www.software.ibm.com/data/db2/udb/ad
****************************************************************************/

#include <sqlsystm.h>
#if (defined(DB2NT))
#include "utilsnap.cxx"
#else // UNIX
#include "utilsnap.C"
#endif

class InstanceSnapshot
{
  public:
    int GetInstanceSnapshotNew();
};

/***************************************************************************/
/* GetInstanceSnapshotNew                                                  */
/* Construct new request stream with values that tell the db2GetSnapshot   */
/* capture an instance level snapshot. Then pass the sqlma to the          */
/* GetSnapshot function in utilsnap.C, which captures the snapshot (using  */
/* the db2GetSnapshot API) and prints the monitor data.                    */
/***************************************************************************/
int InstanceSnapshot::GetInstanceSnapshotNew()
{
  int rc = 0;                   // return code
  Snapshot snapshot;            // Snapshot object
  db2AddSnapshotRqstData snapReq;
  struct sqlca sqlca;

  memset(&snapReq, 0, sizeof(snapReq));
  memset(&sqlca  , 0, sizeof(sqlca));

  snapReq.pioRequestData = NULL;
  snapReq.iRequestType   = SQLMA_DB2;
  rc = db2AddSnapshotRequest(db2Version900, &snapReq, &sqlca);
  if (rc) goto exit;

  rc = snapshot.GetSnapshotNew(&snapReq);

exit:

  return rc;
} //InstanceSnapshot::GetInstanceSnapshotNew

int main(int argc, char *argv[])
{
  int rc = 0;
  char nodeName[SQL_INSTNAME_SZ + 1];
  char user[USERID_SZ + 1];
  char pswd[PSWD_SZ + 1];
  CmdLineArgs check;            // command line arguments object
  Instance inst;                // Instance object
  InstanceSnapshot instsnapshot;    // Snapshot object
  Snapshot snapshot;

  // check the command line arguments
  rc = check.CmdLineArgsCheck2(argc, argv, inst);
  if (rc != 0) return rc;

  cout << "\nTHIS SAMPLE SHOWS HOW TO GET AN INSTANCE LEVEL SNAPSHOT.\n";

  // attach to a local or remote instance
  rc = inst.Attach();
  if (rc != 0) return rc;

  // turn on all the monitor switches
  rc = snapshot.TurnOnAllMonitorSwitches();
  if (rc != 0) return rc;

  // update UTIL_IMPACT_LIM parameter 
  rc = snapshot.UpdateUtilImpactLim();
  if (rc != 0) return rc;

  // capture a snapshot at the instance level and print the monitor data
  rc = instsnapshot.GetInstanceSnapshotNew();

  // detach from the local or remote instance
  rc = inst.Detach();

  return rc;
} // main
